package gui.productgroup;

import facades.StorageFacade;
import gui.common.*;
import gui.inventory.*;
import gui.product.AddProductController;

import java.util.Observer;
import java.util.Observable;

import coreModel.HIT;
import coreModel.ItemManager;
import coreModel.ProductContainer;
import coreModel.ProductGroup;
import coreModel.ProductManager;
import coreModel.StorageUnitManager;
import coreModel.Supply;
import coreModel.Unit;

/**
 * Controller class for the add product group view.
 */
public class AddProductGroupController extends Controller implements
		IAddProductGroupController {
	
	
	private ProductContainerData selectedPC;
	
	
	/**
	 * Constructor.
	 * 
	 * @param view Reference to add product group view
	 * @param container Product container to which the new product group is being added
	 */
	public AddProductGroupController(IView view, ProductContainerData container) {
		super(view);
		construct();
		
		selectedPC = container;
		
		this.getView().enableOK(false);
		this.getView().setSupplyValue("0");
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IAddProductGroupView getView() {
		return (IAddProductGroupView)super.getView();
	}
	
	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() 
	{
		this.getView().enableOK(false);
		
		// make sure the name is not empty
		String name = this.getView().getProductGroupName();
		if ( name.length() == 0 )
			return;
		
		ProductContainer tmpPC;
		if ( !selectedPC.hasTag() )
		{
			System.out.println("AddProductGroupController: Error, the selected " + 
					"ProductContainerData does not have a model equivalent tag!");
			return;
		}
		else
		{
			tmpPC = (ProductContainer) selectedPC.getTag();
		}
		
		if (!tmpPC.canAddProductGroup(name))
			return;
		
		// parse the supply value and make sure it creates a valid Supply object
		try
		{
			Unit u = DataConverter.toEnum( this.getView().getSupplyUnit() );
			String supVal = this.getView().getSupplyValue();
			if ( this.getView().getSupplyUnit() == SizeUnits.Count)
			{
				Supply s = new Supply(Integer.parseInt(supVal), u) ;
			}
			else
			{
				Supply s = new Supply(Float.parseFloat(supVal), u);
			}
			
		}
		catch (Exception e)
		{
			return;
		}
		
		
		this.getView().enableOK(true);
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() 
	{
		
	}

	//
	// IAddProductGroupController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * add product group view is changed by the user.
	 */
	@Override
	public void valuesChanged() 
	{
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the add product group view.
	 */
	@Override
	public void addProductGroup() 
	{
		float val = Float.parseFloat( this.getView().getSupplyValue() );
		Unit unit = DataConverter.toEnum( this.getView().getSupplyUnit() );
		
		String name = this.getView().getProductGroupName();
		ProductContainer parent = (ProductContainer)selectedPC.getTag();
		
		Supply sup = null;
		ProductGroup newPG =null;
		try
		{
			sup = new Supply(val, unit);
			newPG = new ProductGroup(name, parent, sup);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		// This code was added by Ammon to maintain selected units
		// -----------------------------
		ProductContainerData tmpPCD = new ProductContainerData();
		tmpPCD.setTag(newPG);
		GUISelections.getInstance().setContainer( tmpPCD );
		// -----------------------------
		
		StorageFacade.getInstance().addProductGroup(newPG, parent);
	}
	

}

